//
//  BSSFeedbackViewController.m
//  PreschoolEducation
//
//  Created by 传铎 on 2020/11/16.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSFeedbackViewController.h"
#import "BSSAddPhotosCollectionViewController.h"
#import "PRPlaceHolderTextView.h"
@interface BSSFeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic ,strong) NSMutableArray *photoArr;
@property (nonatomic ,strong) NSMutableArray *deleUrlArr;
@property (nonatomic ,strong) PRPlaceHolderTextView *opinionTextV;
@property (nonatomic ,strong) UITextField *opinionTF;
@property (nonatomic ,copy) NSString *prevText;
@property (nonatomic ,strong) UISwitch * switchBtn;

@end

@implementation BSSFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"意见反馈";
    self.navBar.titleLabel.textColor = colorCode(@"333333");
    _deleUrlArr = [NSMutableArray array];
    _photoArr = [NSMutableArray array];
    self.view.backgroundColor = colorCode(@"f2f2f2");
    [self creatUI];
}
- (void)creatUI{
    UIView *detailsView = [[UIView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, 80)];
    detailsView.backgroundColor = colorCode(@"ffffff");
    [self.view addSubview:detailsView];
    
    UIImageView *titleImageV = [[UIImageView alloc]init];
    titleImageV.image = imageNameStr(@"BSSMessageBoard_message");
    [detailsView addSubview:titleImageV];
    [titleImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.top.mas_equalTo(17);
        make.width.mas_equalTo(49);
        make.height.mas_equalTo(50);
    }];
    
    UILabel *detailsLabel = [[UILabel alloc]init];
    detailsLabel.text = @"非常期待能够得到您关于本社区的任何宝贵意见和建议，社区管理员会在3个工作日内对留言进行回复，请耐心等待。";
    detailsLabel.textColor = colorCode(@"#666666");
    detailsLabel.font =  fontCount(14);
    detailsLabel.numberOfLines = 0;
    [detailsView addSubview:detailsLabel];
    
    [detailsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(titleImageV.mas_right).offset(16);
        make.right.mas_equalTo(-24);
    }];
    
    UIView *opinionView = [[UIView alloc]init];
    opinionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:opinionView];
    [opinionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(detailsView.mas_bottom).offset(10);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(320);
    }];
    self.opinionTextV = [[PRPlaceHolderTextView alloc]initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH - 30, 140)];
    self.opinionTextV.placeholder = @"请输入遇到的问题或建议";
    self.opinionTextV.delegate = self;
    self.opinionTextV.font = [UIFont systemFontOfSize:17];
    [opinionView addSubview:self.opinionTextV];
    // Do any additional setup after loading the view.
    
    BSSAddPhotosCollectionViewController * AddPhotosCollectionView = [[BSSAddPhotosCollectionViewController alloc]init];;
    
    [self addChildViewController:AddPhotosCollectionView];
    WEAKSELF;
    BSSAddPhotosCollectionViewController *vc = [self.childViewControllers firstObject];
    vc.chooseMessageBoardPicture = ^(NSMutableArray * _Nonnull array, NSMutableArray * _Nonnull deleArr) {
        [weakSelf.photoArr removeAllObjects];
        [weakSelf.photoArr addObjectsFromArray:array];
        [weakSelf.deleUrlArr removeAllObjects];
        [weakSelf.deleUrlArr addObjectsFromArray:deleArr];
    };
    vc.view.frame = CGRectMake(0, 160, SCREEN_WIDTH, 100);
    [opinionView addSubview:vc.view];
    UIView *diView1 = [[UIView alloc]init];
    diView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:diView1];
    [diView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(opinionView.mas_bottom).offset(13);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
    }];
    UIView *diView2 = [[UIView alloc]init];
    [opinionView addSubview:diView2];
    diView2.backgroundColor = colorCode(@"ffffff");
    
    [diView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    UIView *lineView= [[UIView alloc]initWithFrame:CGRectMake(24, 260, SCREEN_WIDTH - 48, 1)];
    [opinionView addSubview:lineView];
    lineView.backgroundColor = colorCode(@"f2f2f2");
    
    UILabel *leaveMessageLabel = [[UILabel alloc]init];
    leaveMessageLabel.textColor = colorCode(@"#585858");
    leaveMessageLabel.text = @"匿名留言";
    leaveMessageLabel.font = fontCount(14);
    [opinionView addSubview:leaveMessageLabel];
    [leaveMessageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView.mas_bottom);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(24);
    }];
    
    _switchBtn = [[UISwitch alloc]init];
    _switchBtn.onTintColor = colorCode(@"#BB1B21");
    [_switchBtn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [opinionView addSubview:_switchBtn];
    
    [_switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-24);
        make.centerY.mas_equalTo(leaveMessageLabel.mas_centerY).offset(10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(24, 0, 75, 45)];
    label.text = @"联系方式";
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorFromHexCode:@"#585858"];
    [diView1 addSubview:label];
    _opinionTF = [[UITextField alloc]initWithFrame:CGRectMake(109, 0, SCREEN_WIDTH - 133, 45)];
    _opinionTF.textAlignment = NSTextAlignmentRight;
//    _opinionTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 44)];
//    _opinionTF.leftViewMode = UITextFieldViewModeAlways;
//    _opinionTF.layer.borderColor = COLORRGB(154, 159, 169).CGColor;
//    _opinionTF.layer.borderWidth = 1.0f;
    _opinionTF.delegate = self;
    _opinionTF.font = [UIFont systemFontOfSize:14];
    _opinionTF.keyboardType = UIKeyboardTypeNumberPad;
    _opinionTF.placeholder = @"选填，便于我们联系您";
    [diView1 addSubview:_opinionTF];
    UIButton *submitBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBnt setBackgroundColor:colorCode(@"#FE8300")];
    [submitBnt setTitle:@"提交" forState:0];
    submitBnt.layer.masksToBounds = YES;
    submitBnt.layer.cornerRadius = 5;
    [submitBnt addTarget:self action:@selector(submitClick:) forControlEvents:UIControlEventTouchUpInside];
    [submitBnt setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:submitBnt];
    
    [submitBnt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(diView1.mas_bottom).offset(30);
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.height.mas_equalTo(50);
    }];
}
- (void)switchAction:(UISwitch *)switchButton
{
//    switchButton.on = !switchButton.on;
}
- (void)submitClick:(UIButton *)sender{
    if(IsEmpty(_opinionTextV.text)){
        [MBProgressHUD showText:@"请输入遇到的问题或建议"];
        return ;
    }
    
    if (!IsEmpty(_opinionTF.text)) {
        if (_opinionTF.text.length != 11) {
            [MBProgressHUD showText:@"请输入正确的手机号"];
            return ;
        }
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if (!IsEmpty(_opinionTF.text)) {
        [dict setValue:_opinionTF.text forKey:@"mobile"];
    }
    if (_switchBtn.on) {
        [dict setValue:@"1" forKey:@"isAnonymous"];
    }else{
        [dict setValue:@"0" forKey:@"isAnonymous"];
    }
    NSString *deleteUrls = @"";
    NSString *imgUrls = @"";
    if (!IsEmpty(_photoArr)) {
        for (int i = 0; i < _photoArr.count; i ++) {
            if (i == 0) {
                imgUrls = _photoArr[0];
            }else{
                imgUrls = [NSString stringWithFormat:@"%@,%@",imgUrls,_photoArr[i]];
            }
        }
    }
    if (!IsEmpty(_deleUrlArr)) {
        for (int i = 0; i < _deleUrlArr.count; i ++) {
            if (i == 0) {
                deleteUrls = _deleUrlArr[0];
            }else{
                deleteUrls = [NSString stringWithFormat:@"%@,%@",imgUrls,_deleUrlArr[i]];
            }
        }
    }
    [dict setValue:imgUrls forKey:@"imgUrls"];
    [dict setValue:deleteUrls forKey:@"deleteUrls"];
    [dict setValue:self.opinionTextV.text forKey:@"content"];
    BSSRequest * request = [[BSSRequest alloc] initWithURL:@"" dic:dict];
    [request startRequest:^(BSSRequestStatus requestStatus, NSString * _Nullable message, id  _Nullable responseObject) {
        if (requestStatus == BSSRequestStatusSuccess) {
            
        } else {
            [MBProgressHUD showText:message];
            
        }
    }];
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@""])
    {
        return YES;
    }
    if(textField.text.length >= 11){
        return NO;
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    // 不让输入表情
    if ([textView isFirstResponder]) {
        if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
            return NO;
        }
    }
    if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
        return NO;
    }
    NSInteger afterChangeLength=[textView.text length]+[text length]-range.length;
    //NSLog(@"limit=%d, curopLen=%d", inputLimit, afterChangeLength);
    
    if(afterChangeLength> 499){
        return NO;
    }
    return YES;
    
    
}

- (void)textViewDidChange:(UITextView *)textView
    {
        if ([textView.text containEmoji]) {
            NSUInteger location = [textView selectedRange].location - 2 ;
            textView.text =  _prevText;
            if (location > textView.text.length) {
                location = textView.text.length;
            }
            [textView setSelectedRange:NSMakeRange(location, 0)];
        }else {
            _prevText = textView.text;
        }
        //该判断用于联想输入
        if (textView.text.length > 500)
        {
            textView.text = [textView.text substringToIndex:500];
        }
        
}

@end
